function isObject(obj){
  return typeof obj === 'object' && obj !== null
}

function isEqual(obj1, obj2) {
  if(!isObject(obj1) || !isObject(obj2)){
    // 值类型
    return obj1 === obj2
  }
  if(obj1 === obj2){
    return true
  }
  // 两个都是对象或者数组，而且不相等

  // 1. 先判断keys个数
  const obj1Keys = Object.keys(obj1)
  const obj2Keys = Object.keys(obj2)
  if (obj1Keys.length !== obj2Keys.length) {
    return false
  }
  // 2. 以obj1为基准，和obj2 一次递归比较
  for (let key in obj1) {
    // 递归比较
    const res = isEqual(obj1[key], obj2[key])
    if (!res) {
      return false
    }
  }
  return true
}

const obj1 = {
  a: 100,
  b: {
    x: 100,
    y: 200
  }
}
const obj2 = {
  a: 100,
  b: {
    x: 100,
    y: 200,
    z: [2,3,4]
  }
}
const obj4 = {
  a: 100,
  b: {
    x: 100,
    y: 200,
    z: [2,3,4]
  }
}
const obj3 = obj2;

console.log(isEqual(obj3, obj2))
console.log('isEqual2:', isEqual2(obj1, obj2)) // false
console.log('isEqual2:', isEqual2(obj2, obj3)) // true
console.log('isEqual2:', isEqual2(obj2, obj4)) // true

function isObj(obj) {
  return typeof obj === 'object' && obj !== null
}
function isEqual2(obj1, obj2) {
  if (!isObj(obj1) || !isObj(obj2))return obj1 === obj2
  if(obj1 === obj2)return true

  if(Object.keys(obj1).length !== Object.keys(obj2).length)return false

  if((obj1 instanceof Array) && (obj2 instanceof Array)) {
    for(let key in obj1) {
      const res = isEqual2(obj1[key], obj2[key])
      if(!res)return false
    }
    return true
  }

  if((obj1 instanceof Object) && (obj2 instanceof Object)) {
    for(let key in obj1) {
      const res = isEqual2(obj1[key], obj2[key])
      if(!res)return false
    }
    return true
  }

  return false
}